package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.admin.dao.DynamicTopicDao;
import com.ytjj.qmyx.admin.model.request.DynamicTopicRequest;
import com.ytjj.qmyx.admin.model.response.DynamicTopicResponse;
import com.ytjj.qmyx.admin.service.DynamicTopicService;
import com.ytjj.qmyx.mall.mapper.DynamicTopicMapper;
import com.ytjj.qmyx.mall.model.DynamicTopic;
import com.ytjj.qmyx.mall.model.DynamicTopicExample;
import com.ytjj.qmyx.users.client.BigdataClient;
import com.ytjj.qmyx.users.model.bigdata.AdsTopicResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 *
 * 
 * @description:
 */
@Service
@Slf4j
public class DynamicTopicServiceImpl implements DynamicTopicService {

    @Resource
    DynamicTopicMapper dynamicTopicMapper;

    @Autowired
    RedisService redisService;

    @Resource
    DynamicTopicDao dynamicTopicDao;

    @Autowired
    private BigdataClient bigdataClient;


    @Override
    public Integer addOrEditTopic(DynamicTopic dynamicTopic) {
        if(dynamicTopic.getId() == null){//新增
            dynamicTopic.setCreateTime(new Date());
            dynamicTopic.setUpdateTime(new Date());
            return dynamicTopicMapper.insertSelective(dynamicTopic);
        }else{//修改
            DynamicTopic newObj = dynamicTopicMapper.selectByPrimaryKey(dynamicTopic.getId());
            newObj.setTitle(dynamicTopic.getTitle());
            newObj.setId(dynamicTopic.getId());
            newObj.setColorMark(dynamicTopic.getColorMark());
            newObj.setContent(dynamicTopic.getContent());
            newObj.setInitNum(dynamicTopic.getInitNum());
            newObj.setStatus(dynamicTopic.getStatus());
            newObj.setLabelUrl(StringUtils.isEmpty(dynamicTopic.getLabelUrl()) ? null : dynamicTopic.getLabelUrl());
            newObj.setShareImgUrl(dynamicTopic.getShareImgUrl());
            newObj.setTopicImgUrl(dynamicTopic.getTopicImgUrl());
            newObj.setTopicShrinkImgUrl(dynamicTopic.getTopicShrinkImgUrl());
            newObj.setSort(dynamicTopic.getSort());
            newObj.setShareTitle(dynamicTopic.getShareTitle());
            newObj.setColorMark(dynamicTopic.getColorMark());
            return dynamicTopicMapper.updateByPrimaryKey(newObj);
        }
    }

    @Override
    public Integer setOffOrOnForTopic(Integer id, Integer status) {
        DynamicTopic dynamicTopic = new DynamicTopic();
        dynamicTopic.setId(id);
        dynamicTopic.setStatus(status);
        return  dynamicTopicMapper.updateByPrimaryKeySelective(dynamicTopic);
    }

    @Override
    public CommonPage getDynamicTopicDetails(DynamicTopicRequest dynamicTopicRequest) {
        PageHelper.startPage(dynamicTopicRequest.getPageNum(),dynamicTopicRequest.getPageSize());
        List<DynamicTopicResponse> dynamicTopicResponses = dynamicTopicDao.selectByParams(dynamicTopicRequest);
        for (DynamicTopicResponse item : dynamicTopicResponses) {
            AdsTopicResponse totalPv =  bigdataClient.getTotalById(item.getId());
            AdsTopicResponse todayPv =  bigdataClient.getTodayPv(item.getId());
            item.setTodayTopicPvNum(todayPv == null ? 0 : todayPv.getPv());
            item.setTodayDynamicPvNum(todayPv == null ? 0 : todayPv.getEvaPv());
            //真实pv
            item.setTotalTopicPvNum(totalPv == null ? 0 : totalPv.getPvTotal());
            //真实pv
            item.setTotalDynamicPvNum(totalPv == null ? 0 : totalPv.getEvaPvTotal());
        }
        return CommonPage.restPage(dynamicTopicResponses);
    }

    @Override
    public List<DynamicTopicResponse> getAllTopic(String title) {
        return dynamicTopicDao.getListByTitle(title);
    }

    /**
     * 这个方法实际没用了，因为直接走bigdata服务了
     * @return
     */
    @Override
    public List<AdsTopicResponse> getTopicsTop() {
//        List<AdsTopicResponse> list = bigdataClient.topList();
//        return adsTopicDao.getTopicsTop();
        return null;
    }

    @Override
    public AdsTopicResponse getTopicInfoAndPvById(Integer id) {
        AdsTopicResponse topicsPvById = bigdataClient.getTopicInfoAndPvById(id);
        DynamicTopicExample dynamicTopicExample = new DynamicTopicExample();
        if(topicsPvById == null) {
            dynamicTopicExample.createCriteria().andIdEqualTo(id).andStatusEqualTo(0);
        }
        else {
            dynamicTopicExample.createCriteria().andIdEqualTo(topicsPvById.getId()).andStatusEqualTo(0);
        }
        List<DynamicTopic> dynamicTopics = dynamicTopicMapper.selectByExample(dynamicTopicExample);
        if(dynamicTopics.size() != 1){
            throw  new ApiException("话题异常");
        }
        AdsTopicResponse adsTopicResponse = new AdsTopicResponse();
        BeanUtils.copyProperties(dynamicTopics.get(0),adsTopicResponse);
        adsTopicResponse.setPvTotal(topicsPvById == null ? adsTopicResponse.getInitNum() : topicsPvById.getPvTotal()+adsTopicResponse.getInitNum()+topicsPvById.getPvTotalFake());
        return adsTopicResponse;
    }

    @Override
    public List<DynamicTopic> getTopicInfoList() {
        DynamicTopicExample dynamicTopicExample = new DynamicTopicExample();
        dynamicTopicExample.createCriteria().andStatusEqualTo(0);
        List<DynamicTopic> dynamicTopics = dynamicTopicMapper.selectByExample(dynamicTopicExample);
        return dynamicTopics;
    }
}
